1 00:00:00,960 --> 00:00:01,710 Hey there. 2 00:00:01,740 --> 00:00:07,020 The next thing that we can go ahead and script in our game is going to be the money service here on 3 00:00:07,020 --> 00:00:07,890 the server. 4 00:00:07,890 --> 00:00:12,720 This service is going to be responsible for randomly placing valuable items around the map that players 5 00:00:12,720 --> 00:00:14,220 can collect to earn money. 6 00:00:14,580 --> 00:00:20,940 So inside of server storage, we have this folder called money items that hold different types of items 7 00:00:20,940 --> 00:00:25,200 that the players can collect, as well as the different positions on the map that the money items can 8 00:00:25,200 --> 00:00:25,830 spawn in. 9 00:00:25,830 --> 00:00:30,330 So let me go ahead and drag this folder into the workspace so we can actually see all of these. 10 00:00:30,330 --> 00:00:34,230 And here are all of the different money items that players can collect. 11 00:00:34,230 --> 00:00:39,960 They can collect a stack of gold bars, a singular gold bar, a singular silver bar, a stack of silver 12 00:00:39,960 --> 00:00:40,560 bars. 13 00:00:40,560 --> 00:00:46,200 We have a pile of coins, a singular coin, a pile of dollars, and a singular dollar. 14 00:00:46,200 --> 00:00:47,550 Inside of here as well. 15 00:00:47,550 --> 00:00:51,330 We have a whole bunch of positions around the map where these items can spawn in. 16 00:00:51,330 --> 00:00:53,910 So as you can see, one can spawn right here. 17 00:00:53,910 --> 00:00:57,840 One can spawn over here, over here, one on top of here. 18 00:00:57,840 --> 00:01:02,520 And there's also a few other hidden positions around the map where these different money items can spawn 19 00:01:02,520 --> 00:01:06,360 in, such as up here or over here. 20 00:01:06,360 --> 00:01:09,930 And then I have one up here and things kind of like that. 21 00:01:09,930 --> 00:01:13,980 And as well, if we go into the basement, there's a whole bunch of other positions spread throughout 22 00:01:13,980 --> 00:01:16,590 here where our different money items can spawn at. 23 00:01:16,590 --> 00:01:23,580 These different positions also have attributes on them that tell us which spawn value of money item 24 00:01:23,580 --> 00:01:24,600 can spawn here. 25 00:01:24,600 --> 00:01:32,100 So for example, at this position here, hidden back here, a spawn value of high or basically any of 26 00:01:32,100 --> 00:01:36,120 the items here, like the gold pile or the gold bar can spawn there. 27 00:01:36,240 --> 00:01:41,940 So we basically will have three different ranks of money items, ones that are low value, ones that 28 00:01:41,940 --> 00:01:44,760 are medium value and then ones that are high value. 29 00:01:44,760 --> 00:01:47,940 So these ones right here would be considered like high value. 30 00:01:47,940 --> 00:01:50,910 I guess these ones here would be considered medium value. 31 00:01:50,910 --> 00:01:53,400 And then we could have low value items over here. 32 00:01:53,670 --> 00:01:57,870 So each of these different parts we set a different spawn value on them. 33 00:01:58,530 --> 00:02:05,670 And then any areas where we want a money item, regardless of the value to spawn there we marked as 34 00:02:05,670 --> 00:02:06,360 any. 35 00:02:06,450 --> 00:02:11,880 So the plan is, is that our money service is going to basically loop through all of these different 36 00:02:11,880 --> 00:02:18,900 money positions, and it's going to randomly copy one of these money items and place them at this position. 37 00:02:18,930 --> 00:02:24,390 Now, since our money items have many different shapes and sizes, we're going to have to figure out 38 00:02:24,390 --> 00:02:30,270 how to position these parts at these positions here while keeping them flush with the surface. 39 00:02:30,270 --> 00:02:36,120 Because if we were to move this model here to this position, well, the model will be clipping through 40 00:02:36,120 --> 00:02:38,280 this chest right here. 41 00:02:38,280 --> 00:02:43,290 And some of the items may not be perfectly flush with the surface to make it look like they're actually 42 00:02:43,290 --> 00:02:44,670 sitting on the surface. 43 00:02:44,670 --> 00:02:49,770 So we're going to have to use a little bit of math to position them correctly on these different positions. 44 00:02:49,770 --> 00:02:54,750 So we can go ahead and take this folder and put it back into server storage. 45 00:02:54,750 --> 00:02:58,260 And we can go ahead and get started with our money service. 46 00:02:58,260 --> 00:03:00,390 So we're going to need three services in here. 47 00:03:00,390 --> 00:03:02,430 One is going to be server storage. 48 00:03:05,110 --> 00:03:06,910 One is going to be replicated. 49 00:03:06,910 --> 00:03:07,810 Storage. 50 00:03:10,260 --> 00:03:13,800 And then we're going to need the server script service. 51 00:03:17,840 --> 00:03:21,770 And then from this point we can create the table that will represent the service. 52 00:03:21,770 --> 00:03:25,070 So money service is equal to a table. 53 00:03:25,070 --> 00:03:27,950 And then at the end we can return money service. 54 00:03:28,100 --> 00:03:32,540 Now some other variables that we're going to need in here are to reference a few events. 55 00:03:32,540 --> 00:03:35,990 One of these events is going to be a game communication. 56 00:03:35,990 --> 00:03:39,410 But this game communication is going to be a bindable event. 57 00:03:39,410 --> 00:03:46,880 So we can call this game comms enum server, because this is going to be a bindable event for different 58 00:03:46,880 --> 00:03:49,880 service scripts to communicate between each other. 59 00:03:49,880 --> 00:03:53,420 So inside of server storage there's a folder called events. 60 00:03:53,420 --> 00:03:56,780 And in there we can get the Bindesboll's dot game comms. 61 00:03:56,840 --> 00:04:02,360 And that means we're going to have to create a different enum to represent the possible actions that 62 00:04:02,360 --> 00:04:05,600 can be done between server scripts. 63 00:04:05,600 --> 00:04:10,340 So this is not a remote event where we're giving actions to clients or clients or giving actions to 64 00:04:10,340 --> 00:04:13,940 the server, but instead this is communication between different server scripts. 65 00:04:14,670 --> 00:04:19,830 So inside of our server script service we have a folder in here called modules. 66 00:04:19,830 --> 00:04:24,480 And in here there is a module for game communication between server scripts. 67 00:04:24,480 --> 00:04:30,360 And now we can basically just go and copy one of the enums from let's say replicated storage. 68 00:04:30,360 --> 00:04:32,700 So that way we don't have to type it all out by hand. 69 00:04:32,700 --> 00:04:35,640 We'll just copy this and then paste it in here. 70 00:04:36,340 --> 00:04:41,620 And what we could do is we could, uh, section out our game communication enum into basically actions 71 00:04:41,620 --> 00:04:43,810 that can be given to different services. 72 00:04:43,810 --> 00:04:48,010 So in this case, the actions we want to define are going to be for our money service. 73 00:04:48,010 --> 00:04:50,620 So we can have a key called money service. 74 00:04:50,620 --> 00:04:55,690 And in here we'll have a table containing all the possible actions that we can give to the money service. 75 00:04:55,690 --> 00:05:01,690 And in this case this is going to be for initializing or basically placing money items around the map. 76 00:05:01,690 --> 00:05:05,500 So we can call this action place money items. 77 00:05:07,830 --> 00:05:09,540 And we should be good to go here. 78 00:05:09,540 --> 00:05:15,000 That means we can go back in our, uh, money service and require this game comms enum so we can call 79 00:05:15,000 --> 00:05:18,030 it Game Comms enum server. 80 00:05:18,240 --> 00:05:25,560 And we'll require from server script service, dot server, dot modules, dot enums dot game communication 81 00:05:25,560 --> 00:05:26,160 enum. 82 00:05:26,310 --> 00:05:30,540 We also want to reference our update guy event and replicated storage. 83 00:05:30,540 --> 00:05:35,400 That way when a player collects money, we can tell them to display that you know, money collection 84 00:05:35,400 --> 00:05:36,840 frame on their screen. 85 00:05:36,960 --> 00:05:41,610 And that also means we're going to have to get our Guy actions enum. 86 00:05:43,830 --> 00:05:44,790 Which is then replicated. 87 00:05:44,790 --> 00:05:49,260 Storage dot modules dot enums dot gui actions enum. 88 00:05:49,320 --> 00:05:53,340 We're also going to be requiring another module script that is inside of the server. 89 00:05:53,340 --> 00:05:55,500 And this module script is called anti-cheat. 90 00:05:55,650 --> 00:06:00,090 And basically what we're going to do in here is we're just going to have one function that module scripts 91 00:06:00,090 --> 00:06:05,610 on the server can require that will allow us to determine the distance that a player is away from. 92 00:06:05,610 --> 00:06:09,570 Like maybe let's say a proximity prompt or a click detector or whatever. 93 00:06:09,570 --> 00:06:16,140 This is, to verify that the player is actually within range of a certain click detector or a proximity 94 00:06:16,140 --> 00:06:21,330 prompt to make sure that they aren't exploiting and activating click detectors or prompts from all the 95 00:06:21,330 --> 00:06:22,920 way across the map. 96 00:06:22,920 --> 00:06:29,250 So we can go ahead and call this anti-cheat and we'll require it from server script service. 97 00:06:29,460 --> 00:06:29,700 Oops. 98 00:06:29,700 --> 00:06:31,620 Let me rename that with the T as well. 99 00:06:31,620 --> 00:06:32,670 Don't want to forget our T. 100 00:06:34,650 --> 00:06:37,800 Dot server, dot modules, dot anti-cheat. 101 00:06:38,220 --> 00:06:43,890 And then inside of our anti-cheat module script, all we'll have is a variable to represent our anti-cheat. 102 00:06:44,710 --> 00:06:47,830 Make sure to return it at the end of our module script. 103 00:06:47,830 --> 00:06:53,020 And then the only function that we're going to have inside of our anti-cheat is to check a player's 104 00:06:53,020 --> 00:06:59,110 distance to a particular object or part in the workspace, so we can have this function be passed a 105 00:06:59,110 --> 00:06:59,830 player. 106 00:07:00,730 --> 00:07:04,630 An object that we want to compare the distance to. 107 00:07:05,360 --> 00:07:11,060 And then we actually need the value to compare this so we can call it max distance. 108 00:07:12,160 --> 00:07:15,790 And basically what we'll do is we'll get the character of this player. 109 00:07:17,030 --> 00:07:21,380 If they don't have a character, then we're just going to return false. 110 00:07:22,240 --> 00:07:26,950 Otherwise, we can get the distance between the player's character and this object. 111 00:07:27,460 --> 00:07:33,550 By getting the position of the object and subtracting it by the character. 112 00:07:33,550 --> 00:07:33,820 Dot. 113 00:07:33,820 --> 00:07:35,050 Humanoid route. 114 00:07:35,050 --> 00:07:36,850 Part dot position. 115 00:07:37,980 --> 00:07:42,180 So this will give us a vector three that represents the distance between these two objects. 116 00:07:42,180 --> 00:07:47,130 And then we can just get the magnitude of it, which will give us a number that represents how far away 117 00:07:47,130 --> 00:07:50,220 this object is from our humanoid root part in studs. 118 00:07:50,430 --> 00:07:57,030 Now, if this distance, uh, is less than or equal to our max distance or the threshold number that 119 00:07:57,030 --> 00:08:00,810 we determined here, then we're going to return false to say, nope, they're not cheating. 120 00:08:00,840 --> 00:08:07,290 Otherwise, if somehow their distance is greater than our max distance, then there's something weird 121 00:08:07,290 --> 00:08:09,420 going on so we can return true. 122 00:08:09,420 --> 00:08:14,250 And we can say something like unusual client behavior detected. 123 00:08:15,390 --> 00:08:16,800 Now back in our money service. 124 00:08:16,800 --> 00:08:22,860 The next thing that we can define here is a table that basically segregates our money items into three 125 00:08:22,860 --> 00:08:26,610 different categories low value, medium value, and high value. 126 00:08:26,610 --> 00:08:30,930 So we can call this money items equal to a table. 127 00:08:30,930 --> 00:08:33,090 And then we can have three different categories. 128 00:08:33,090 --> 00:08:34,980 So one is going to be high value. 129 00:08:35,010 --> 00:08:39,060 We'll have medium value and then we'll have low value. 130 00:08:39,580 --> 00:08:44,980 Now, of course, we want there to be a limit for how many different high value, medium value and low 131 00:08:44,980 --> 00:08:47,050 value items we can place on the map. 132 00:08:47,050 --> 00:08:52,900 So inside of each of these tables, we can create a boolean to represent whether or not we are out of 133 00:08:52,900 --> 00:08:55,930 stock for a high, medium or low value item. 134 00:08:55,930 --> 00:09:02,710 So we can create a key that just says out of stock and we'll set it to false as default. 135 00:09:02,710 --> 00:09:06,220 And I'll just copy this and do this for the rest of the tables. 136 00:09:06,220 --> 00:09:08,110 So out of stock. 137 00:09:08,860 --> 00:09:10,300 And then out of stock. 138 00:09:10,300 --> 00:09:16,540 And then we can go ahead and also put in here the different high value items that exist. 139 00:09:16,540 --> 00:09:22,630 And we want to make sure that the names or the keys of these different items actually match the same 140 00:09:22,630 --> 00:09:24,190 names as our money items. 141 00:09:24,190 --> 00:09:27,130 So we'll take a look at each different money item here. 142 00:09:27,580 --> 00:09:32,290 And we've already determined their value already here in an attribute. 143 00:09:32,290 --> 00:09:36,310 So for example, our gold bar has a high spawn value. 144 00:09:36,310 --> 00:09:40,960 And we've also set an attribute for how much money should be given to a player. 145 00:09:41,680 --> 00:09:43,990 A gold pile is also worth a lot. 146 00:09:44,200 --> 00:09:51,010 Um, and then, for example, our silver pile is medium value and things like that. 147 00:09:51,010 --> 00:09:55,810 So one of the high value items, of course, is our gold pile. 148 00:09:56,470 --> 00:09:58,810 And we'll make another table in here. 149 00:09:58,810 --> 00:10:03,760 And in here we'll store the model for the gold pile which is in server storage. 150 00:10:03,760 --> 00:10:06,070 Dot money items dot gold pile. 151 00:10:06,960 --> 00:10:11,370 We will create a key to represent how many were allowed to place on the map. 152 00:10:11,370 --> 00:10:13,590 So we can call this max allowed. 153 00:10:13,590 --> 00:10:18,660 And the maximum allowed of gold piles that I want on the map are going to be two. 154 00:10:18,870 --> 00:10:23,460 And then we need to keep track of how many of these gold piles are actually on the map. 155 00:10:23,460 --> 00:10:27,000 So we can have another key called currently on map. 156 00:10:27,000 --> 00:10:30,810 And right now that's going to be zero because we haven't spawned any in. 157 00:10:30,810 --> 00:10:38,310 And then basically we can copy this and do the exact same thing for the gold bar and then make a reference 158 00:10:38,310 --> 00:10:41,460 to our gold bar, the Max allowed on the map. 159 00:10:41,460 --> 00:10:43,650 We could do something like four for this one. 160 00:10:44,700 --> 00:10:47,790 And then basically we're just going to continue copying this. 161 00:10:47,790 --> 00:10:50,040 And then we could do it for our median value. 162 00:10:50,070 --> 00:10:56,130 We'll put a key in here that represents for example our coin pile is a median value. 163 00:10:56,550 --> 00:10:58,980 And we'll make a reference to our coin pile. 164 00:11:00,320 --> 00:11:01,670 The max allowed on the map. 165 00:11:01,670 --> 00:11:08,090 We could do something like eight, and then the next, uh, medium value item is our money pile. 166 00:11:08,980 --> 00:11:10,780 We'll make a reference to that as well. 167 00:11:10,780 --> 00:11:11,890 Money pile. 168 00:11:13,680 --> 00:11:16,500 We can also have a max of eight of those on the map. 169 00:11:16,500 --> 00:11:21,750 And then the last medium value item is our silver bar pile. 170 00:11:21,750 --> 00:11:23,820 So we could call it silver pile. 171 00:11:24,880 --> 00:11:30,100 And again, we'll make a reference to our silver pile, and the max of those will allow on the map we 172 00:11:30,100 --> 00:11:31,930 could do something like five. 173 00:11:31,930 --> 00:11:36,670 Then we can copy all of these again and fill out the low value section. 174 00:11:36,670 --> 00:11:42,400 So one of the low value section items is just going to be money, which will just represent a dollar 175 00:11:42,400 --> 00:11:42,670 bill. 176 00:11:42,670 --> 00:11:45,580 So it's called money the max allowed on the map. 177 00:11:45,580 --> 00:11:47,590 We could do something like 15. 178 00:11:48,130 --> 00:11:54,550 We'll have I believe a coin is our next low value item. 179 00:11:54,820 --> 00:11:57,070 So we'll refer to our coin. 180 00:11:57,070 --> 00:11:59,860 The max will allow on the map we could do 15. 181 00:12:00,490 --> 00:12:05,860 And then the last low value item is going to be our silver bar. 182 00:12:07,070 --> 00:12:09,230 And we'll make a reference to that as well. 183 00:12:09,230 --> 00:12:13,430 Silver bar and the Max will allow on the map would be ten. 184 00:12:13,430 --> 00:12:18,620 Last but not least, we need to make a reference to all of the different money item positions inside 185 00:12:18,620 --> 00:12:20,000 of server storage. 186 00:12:20,000 --> 00:12:27,350 So we could call this variable money item positions equal to server storage, dot money items, dot 187 00:12:27,350 --> 00:12:28,610 money item positions. 188 00:12:28,610 --> 00:12:32,300 And we want to get all of the children that are inside of that folder. 189 00:12:32,840 --> 00:12:36,080 And we're also going to need a random data type. 190 00:12:36,080 --> 00:12:39,020 Now we're going to need a few private functions. 191 00:12:39,170 --> 00:12:45,170 One of the first private functions we're going to need is for when one of our money items gets clicked. 192 00:12:45,170 --> 00:12:50,390 So inside each of our money items we have a click part that has a click detector. 193 00:12:50,390 --> 00:12:53,150 And we want to listen for when that click detector gets clicked. 194 00:12:53,150 --> 00:12:57,530 So every time we're spawning these different money items on the map, we want to connect a function 195 00:12:57,530 --> 00:12:59,510 to each of the click detectors. 196 00:12:59,510 --> 00:13:03,740 So we can call this function on money item clicked. 197 00:13:03,740 --> 00:13:07,610 And that will handle, you know, giving players money and things like that. 198 00:13:07,970 --> 00:13:10,070 We're going to need a function. 199 00:13:10,760 --> 00:13:16,490 To set up or basically place all the money items on the map so we can call this function place money 200 00:13:16,490 --> 00:13:19,730 items randomly on map. 201 00:13:20,710 --> 00:13:24,940 And since this function is going to have to loop through many different positions that have different 202 00:13:24,940 --> 00:13:31,600 values, I think it would be best if we create another dedicated function for spawning a money item 203 00:13:31,600 --> 00:13:35,230 or a random money item based on a past value. 204 00:13:35,230 --> 00:13:42,160 So we can have a function called spawn money item, and this function can get past a value which will 205 00:13:42,160 --> 00:13:44,320 be low, medium, high or any. 206 00:13:44,620 --> 00:13:51,700 And we will also get the part or basically the position where we need to place this money item at. 207 00:13:55,140 --> 00:13:57,870 And let me make sure to denote this as a function as well. 208 00:13:58,770 --> 00:14:06,030 Now before this function can actually go ahead and spawn whatever money item on the map, we actually 209 00:14:06,030 --> 00:14:13,020 need to verify that a we have enough stock for this, whatever value that we're trying to spawn a money 210 00:14:13,020 --> 00:14:13,500 item. 211 00:14:13,500 --> 00:14:21,000 And we also got to make sure that we haven't exceeded the max allowed, uh, level or number based on 212 00:14:21,000 --> 00:14:23,370 the money item that we're trying to spawn. 213 00:14:23,490 --> 00:14:26,310 So that means we'll need another function. 214 00:14:26,310 --> 00:14:33,900 And we can call this function something like get available items to spawn of a particular value. 215 00:14:33,900 --> 00:14:36,360 And we'll pass a value to this as well. 216 00:14:36,810 --> 00:14:42,000 So basically when this function gets called it's going to loop through all the positions and spawn a 217 00:14:42,000 --> 00:14:44,160 money item based on a particular value. 218 00:14:44,160 --> 00:14:51,060 And this function is going to verify or basically get all of the items that we can actually spawn on 219 00:14:51,060 --> 00:14:51,750 this value. 220 00:14:51,750 --> 00:14:57,150 So it's going to make sure that, you know we aren't out of stock and that, you know, are currently 221 00:14:57,150 --> 00:15:01,170 on map number, doesn't exceed our max allowed and things like that. 222 00:15:01,170 --> 00:15:05,430 And then when all of that checks out, then it can actually spawn the money items on our map. 223 00:15:05,430 --> 00:15:10,110 Another function we're going to need is to listen to our game comms event. 224 00:15:10,110 --> 00:15:13,350 So we can just call this on game comms event. 225 00:15:13,350 --> 00:15:15,810 We could pass an action and arguments. 226 00:15:15,810 --> 00:15:22,050 And if you remember, the only action we need to listen to for right now is in our game Comms enum server. 227 00:15:22,650 --> 00:15:26,520 And that's going to be money service, not place money items. 228 00:15:26,520 --> 00:15:29,160 And I don't know why I named that enum. 229 00:15:29,160 --> 00:15:31,140 This should be named event. 230 00:15:31,980 --> 00:15:38,760 So basically, when we get told to place money items on the map, we're going to call our place money 231 00:15:38,760 --> 00:15:40,740 items randomly on map function. 232 00:15:41,010 --> 00:15:45,630 And what this function is going to do is it's going to loop through every single different money item 233 00:15:45,630 --> 00:15:46,860 positions on the map. 234 00:15:46,860 --> 00:15:48,840 But we want to do it randomly. 235 00:15:48,840 --> 00:15:54,600 So what we could do is we could have a for loop that starts at an index of one, and we could get the 236 00:15:54,600 --> 00:15:58,260 total number of items within our money item positions table. 237 00:16:00,940 --> 00:16:05,800 And then inside of this loop, what we're going to do is we're going to generate a random integer. 238 00:16:06,430 --> 00:16:12,910 Between, uh, one and the number of elements in our, uh, money items positions table. 239 00:16:12,910 --> 00:16:19,030 Because after we've spawned this item, uh, based on this random position we've grabbed based on this 240 00:16:19,030 --> 00:16:24,850 random number, we're going to remove it from our money item positions table and pass random int at 241 00:16:24,850 --> 00:16:25,570 the end here. 242 00:16:26,360 --> 00:16:30,590 So that way we're not spawning multiple items at the same position. 243 00:16:31,180 --> 00:16:39,160 But basically what we're going to do is we're going to get the part that is stored in this table by 244 00:16:39,160 --> 00:16:41,950 indexing money item positions with this random integer. 245 00:16:41,950 --> 00:16:45,700 So we're going to go ahead and get this position or this part. 246 00:16:46,210 --> 00:16:50,350 And then we can call our spawn money item function. 247 00:16:50,740 --> 00:16:55,210 And we'll pass uh the attribute for this part. 248 00:16:55,210 --> 00:16:59,410 So part get attribute spawn value. 249 00:17:00,010 --> 00:17:04,450 And then we're also going to pass the part itself. 250 00:17:06,270 --> 00:17:08,370 And we should be good to go there. 251 00:17:08,370 --> 00:17:15,210 So then inside of our spawn money item function, what we need to do is we need to get all of the possible 252 00:17:15,210 --> 00:17:18,540 different money items that we can spawn based on this value. 253 00:17:18,540 --> 00:17:23,850 So we create a table called something like Possible objects to spawn. 254 00:17:23,850 --> 00:17:28,980 And this would get returned to us from our get available Items of value function. 255 00:17:28,980 --> 00:17:30,870 And we would pass the value here. 256 00:17:30,900 --> 00:17:33,480 And now let's hop into this function and fill this out. 257 00:17:33,480 --> 00:17:35,850 So what we want to do in here is we want to create a table. 258 00:17:35,850 --> 00:17:39,000 We'll call it possible objects to spawn. 259 00:17:40,410 --> 00:17:44,040 And then what we're going to check is we're going to check the value passed to this function. 260 00:17:44,040 --> 00:17:50,670 If the value is equal to any meaning that we can spawn any type of money item at a particular position, 261 00:17:50,670 --> 00:17:55,830 then what we need to do is we need to loop through our money items table and go through each one of 262 00:17:55,830 --> 00:18:00,810 our different money items, and ensure that we can spawn it by comparing the max allowed to the currently 263 00:18:00,810 --> 00:18:02,070 on map number. 264 00:18:02,880 --> 00:18:09,210 So if the value is equal to any, we need to loop through every single list of items in our money items 265 00:18:09,210 --> 00:18:10,110 table. 266 00:18:11,140 --> 00:18:15,520 And what we need to do with each one of these lists of money items is we need to loop through it as 267 00:18:15,520 --> 00:18:15,820 well. 268 00:18:15,820 --> 00:18:23,800 So for every single money item in our items table, what we need to do is we need to check if this money 269 00:18:23,800 --> 00:18:32,950 item dot currently on map is greater than or equal to the money item dot max allowed number. 270 00:18:32,950 --> 00:18:39,340 If it is, then we're just going to continue looping because we have reached the maximum amount of this 271 00:18:39,340 --> 00:18:41,560 particular money item that could be spawned on the map. 272 00:18:41,560 --> 00:18:48,730 If we haven't, then we can go ahead and insert this money item into our possible objects to spawn table, 273 00:18:48,730 --> 00:18:52,000 and we're going to pass the money item dot model. 274 00:18:52,450 --> 00:18:58,390 Now, if the value passed to this function isn't any and it's a particular value such as low, medium 275 00:18:58,390 --> 00:19:03,610 or high, then what we need to do is we could just basically copy this loop here, and we want to loop 276 00:19:03,610 --> 00:19:07,810 through every single money item inside of our money items table. 277 00:19:07,810 --> 00:19:10,390 And we want to pass the value here. 278 00:19:10,390 --> 00:19:12,460 So we're looping through every single money item. 279 00:19:12,460 --> 00:19:14,920 And we're making sure that we can spawn that money item. 280 00:19:14,920 --> 00:19:18,940 And if we can, we're inserting it into our possible objects to spawn table. 281 00:19:18,940 --> 00:19:24,520 And then once we do that, we can just go ahead and return possible objects to spawn at the end. 282 00:19:24,520 --> 00:19:31,060 So this function will grab for us all of the possible money items that we can spawn based on the value 283 00:19:31,060 --> 00:19:33,250 or the string that was passed to our function. 284 00:19:33,900 --> 00:19:38,040 Anyways, back into our spawn money item function. 285 00:19:38,040 --> 00:19:43,200 Once we actually get all the possible objects we can spawn on the map, the next thing that we need 286 00:19:43,200 --> 00:19:51,750 to do is we actually need to check if the number of possible objects to spawn is greater than zero, 287 00:19:51,750 --> 00:19:57,630 if it is equal to zero, or basically there's nothing in the table, then we need to return out of this 288 00:19:57,630 --> 00:20:03,750 function, because apparently the value that we pass to spawn has already been filled up, and we cannot 289 00:20:03,750 --> 00:20:07,380 spawn any more of those objects, so we need to return. 290 00:20:07,860 --> 00:20:14,100 But if we do have possible objects to spawn, then what we could do is we could create a clone of this 291 00:20:14,100 --> 00:20:18,300 object by going in our possible objects to spawn table. 292 00:20:18,900 --> 00:20:20,880 And then using RNG. 293 00:20:20,910 --> 00:20:25,470 Next integer will pass one to the number of possible objects to spawn. 294 00:20:25,470 --> 00:20:29,820 That way we're spawning a random object and we're going to clone this object. 295 00:20:30,790 --> 00:20:34,720 And then what we're going to do is we're going to go into money items. 296 00:20:36,130 --> 00:20:40,750 And then we're going to get our clone and get the attribute spawn value. 297 00:20:41,170 --> 00:20:43,570 We'll pass the clone dot name. 298 00:20:44,420 --> 00:20:51,380 And then we're going to set the currently on map key value pair plus equal one because we're adding 299 00:20:51,380 --> 00:20:52,640 a new one on the map. 300 00:20:52,730 --> 00:20:56,210 And now here is where we can spawn our clone onto the map. 301 00:20:56,240 --> 00:20:59,690 Thankfully all of our money items are models. 302 00:20:59,690 --> 00:21:03,140 So we can just basically first make sure that our clone is a model. 303 00:21:03,140 --> 00:21:05,870 So if our clone is a model. 304 00:21:06,710 --> 00:21:12,620 And what we need to do is we need to set the keyframe on this model by using the pivot two function. 305 00:21:13,800 --> 00:21:17,760 And inside of here we're going to have to pass a lot, but basically we're going to create a new key 306 00:21:17,760 --> 00:21:18,540 frame. 307 00:21:18,900 --> 00:21:25,620 And the key frame is going to be equal to uh, what we're going to get is we're going to get the part 308 00:21:25,830 --> 00:21:30,000 dot position or basically the part that we need to spawn this clone at. 309 00:21:30,000 --> 00:21:35,130 We get the position of that part, and then we subtract that position by a vector three. 310 00:21:35,820 --> 00:21:39,000 And this vector three is going to be zero on the x axis. 311 00:21:39,000 --> 00:21:45,330 But on the y axis we're going to pass the part dot size dot y and divide that by two. 312 00:21:45,420 --> 00:21:48,360 And then we're going to pass zero for the z axis. 313 00:21:48,450 --> 00:21:54,600 Then we need to add a another offset using vector three dot new. 314 00:21:54,600 --> 00:21:58,020 And this is going to be zero on the x axis. 315 00:21:58,020 --> 00:22:00,360 But we're going to do is we're going to get our clone. 316 00:22:00,360 --> 00:22:06,900 And since it's a model to get the full size of it we use a function called get extents size. 317 00:22:06,900 --> 00:22:12,510 And this returns the size of the smallest bounding box that contains all of the base parts within the 318 00:22:12,510 --> 00:22:13,110 model. 319 00:22:13,110 --> 00:22:16,230 So basically we're just getting the full size of our model. 320 00:22:16,230 --> 00:22:20,550 And then um, we can get the Y position out of this. 321 00:22:20,760 --> 00:22:23,190 And then we want to divide this by two. 322 00:22:23,220 --> 00:22:26,820 So basically we're just getting half the height of our model. 323 00:22:26,820 --> 00:22:29,100 And we're adding this to our key frame. 324 00:22:29,610 --> 00:22:33,240 We'll actually let me draw this out because it would be easier to understand this way. 325 00:22:33,240 --> 00:22:34,740 Let's say we have a part here. 326 00:22:34,740 --> 00:22:37,770 And this is the position where we need to spawn our money item. 327 00:22:37,770 --> 00:22:39,750 And then we have our money item here. 328 00:22:39,750 --> 00:22:47,010 And it's like this size and it has this origin point two first we subtract and we get the position right 329 00:22:47,010 --> 00:22:47,370 here. 330 00:22:47,370 --> 00:22:47,730 Right. 331 00:22:47,730 --> 00:22:53,460 Because we subtracted by half of this positions uh height in its y axis. 332 00:22:53,460 --> 00:23:00,810 Then what we're doing is we're adding the height of our origin point and our other part. 333 00:23:00,810 --> 00:23:04,710 So we're getting half the height of the total size of our model. 334 00:23:04,710 --> 00:23:05,880 Right, which would be this amount. 335 00:23:05,880 --> 00:23:06,840 And we're adding this. 336 00:23:06,840 --> 00:23:12,780 We're basically moving this position to the equivalent position that would be in the center of our object. 337 00:23:12,780 --> 00:23:17,700 And then from that point we can literally just position our object at this new position that we set. 338 00:23:17,700 --> 00:23:22,800 And that means our part would be, you know, completely flush at the correct position wherever this 339 00:23:22,800 --> 00:23:23,970 position was sitting. 340 00:23:24,810 --> 00:23:28,920 And then for the Z position, we can go ahead and make that zero because we're only concerned about 341 00:23:28,920 --> 00:23:30,000 the Y position. 342 00:23:30,180 --> 00:23:37,230 And then once we do that, we could basically just add a C frame angles and randomly rotate this part 343 00:23:37,230 --> 00:23:39,690 around its y axis. 344 00:23:39,690 --> 00:23:42,210 So we'll pass zero for the x axis. 345 00:23:42,210 --> 00:23:45,480 And then we can do math dot rad and do orange. 346 00:23:45,840 --> 00:23:47,310 Next integer. 347 00:23:47,310 --> 00:23:50,970 Pass a number anywhere between -360 and 360. 348 00:23:50,970 --> 00:23:53,100 Because I don't really care how we rotate it. 349 00:23:53,100 --> 00:23:56,160 And then zero for the z axis. 350 00:23:56,550 --> 00:23:58,650 And that's basically all we need to do. 351 00:23:58,650 --> 00:24:04,950 So we're getting a new C frame of a position that's flush with the surface that we want to set our part 352 00:24:04,950 --> 00:24:13,380 on, and then we're adding an offset based on the height on the y axis for our money item divided by 353 00:24:13,380 --> 00:24:13,710 two. 354 00:24:13,740 --> 00:24:17,010 So we get the center and then we just rotate it randomly. 355 00:24:17,010 --> 00:24:24,900 So our model should be perfectly flush with whatever surface we need to position it to. 356 00:24:24,900 --> 00:24:30,150 After that, we can set the clone dot parent equal to the workspace. 357 00:24:30,150 --> 00:24:34,770 And inside the workspace inside of Krusty Krab, there's a folder called Money Items. 358 00:24:35,010 --> 00:24:42,120 And then when our clone dot click part gets clicked, so there's a click detector in there. 359 00:24:42,120 --> 00:24:44,010 We listen for when that gets clicked. 360 00:24:44,010 --> 00:24:47,070 And we're going to connect a lambda function. 361 00:24:47,070 --> 00:24:48,750 We'll get the player that clicked. 362 00:24:48,750 --> 00:24:52,260 And then we can call our money item clicked function. 363 00:24:52,260 --> 00:24:54,450 We can pass the player who clicked. 364 00:24:54,450 --> 00:24:58,890 And then we can also pass the money item itself to this function. 365 00:24:59,130 --> 00:25:03,060 Now we can go ahead and fill out our on money item clicked function. 366 00:25:03,060 --> 00:25:05,940 So up here we get passed a player. 367 00:25:06,800 --> 00:25:09,500 And then we also get past the money object. 368 00:25:10,140 --> 00:25:13,560 And what we're going to do is we're going to check whether or not this player cheated. 369 00:25:13,560 --> 00:25:18,390 So if this is an exploiter trying to collect all the money around the map at once when they're not even 370 00:25:18,390 --> 00:25:25,050 close to one, then we need to kick them so we can check whether or not they cheated and get a message 371 00:25:25,050 --> 00:25:28,950 from our anti-cheat using the check player distanced object. 372 00:25:28,950 --> 00:25:30,420 We're going to pass our player. 373 00:25:30,690 --> 00:25:38,040 We're going to, uh, pass our money object dot click part because that's what the distance we want 374 00:25:38,040 --> 00:25:43,470 to check the player and then the distance or the max distance they're allowed to be away from. 375 00:25:43,470 --> 00:25:55,710 This click part is going to be money object dot click part dot click detector dot max activation distance. 376 00:25:55,860 --> 00:26:01,740 So if somehow the player exceeds that threshold we can check if they cheated. 377 00:26:01,740 --> 00:26:06,930 If they did then we're going to kick this player, give them the message and then just return out of 378 00:26:06,930 --> 00:26:07,800 this function. 379 00:26:07,800 --> 00:26:14,340 Otherwise if they didn't cheat then we can destroy this money object and then we can access this player's 380 00:26:14,340 --> 00:26:21,120 leaderstats folder, get their money int value, and set the value plus equal to the money object. 381 00:26:21,120 --> 00:26:24,060 And actually we should destroy this afterwards. 382 00:26:24,060 --> 00:26:27,060 So that way we can actually get the attribute of the money object. 383 00:26:27,060 --> 00:26:30,150 So money object get attribute. 384 00:26:32,730 --> 00:26:36,450 Money value, so we're giving the player the money they deserve. 385 00:26:37,120 --> 00:26:46,150 And then we can use the update GUI event and fire to this particular player that they successfully collected 386 00:26:46,150 --> 00:26:46,420 money. 387 00:26:46,420 --> 00:26:52,270 So we can do the GUI actions enum of collected money and the amount that they collected. 388 00:26:52,270 --> 00:26:59,770 The number is going to be equal to the money object, get attribute money value. 389 00:26:59,770 --> 00:27:01,900 And again we can actually just create a variable for this. 390 00:27:01,900 --> 00:27:07,510 So we can just call this money value equal to money object get attribute money value. 391 00:27:07,750 --> 00:27:10,990 And then we can destroy the object. 392 00:27:11,830 --> 00:27:16,180 And since we've already stored the value in here, we can just pass that here. 393 00:27:17,310 --> 00:27:20,700 And then we can also pass that here as well. 394 00:27:23,980 --> 00:27:29,740 And actually, one more thing we need to fill out here is we need to put in an initialize function. 395 00:27:29,740 --> 00:27:31,750 So money service init. 396 00:27:31,750 --> 00:27:38,560 What this will do is it'll listen to our game and comms event dot event and connect our on Gamescom 397 00:27:38,560 --> 00:27:39,700 event function. 398 00:27:40,180 --> 00:27:45,940 And in order to test our money service out, since we don't have any scripts currently that fire this 399 00:27:45,940 --> 00:27:53,350 event and pass the action of place money items temporarily, what we could do is we could create a start 400 00:27:53,350 --> 00:27:57,610 function that will just call our place money items randomly on map. 401 00:27:57,610 --> 00:27:59,650 So this is just temporary. 402 00:27:59,650 --> 00:28:05,770 I plan to remove this once we actually have a different script to fire this event, to tell the service 403 00:28:05,770 --> 00:28:07,930 to start placing the money items on the map. 404 00:28:08,290 --> 00:28:10,480 But now we should be able to test this out. 405 00:28:10,570 --> 00:28:12,040 So let's go ahead and see. 406 00:28:13,050 --> 00:28:16,110 Let's just make sure that we didn't have any errors. 407 00:28:16,110 --> 00:28:18,870 So we loaded the money service. 408 00:28:18,870 --> 00:28:22,170 And then did we start? 409 00:28:22,410 --> 00:28:24,000 Failed to start money service. 410 00:28:24,000 --> 00:28:25,260 Actually we did get an error here. 411 00:28:25,260 --> 00:28:27,390 Let's go ahead and see what the problem is. 412 00:28:27,390 --> 00:28:32,700 So on line 119 invalid argument number one see frame expected got vector three. 413 00:28:32,700 --> 00:28:36,750 So let's go ahead and see what the problem is on line 119. 414 00:28:36,750 --> 00:28:42,840 So if we go to our money service line 119 apparently we're getting an error here. 415 00:28:44,140 --> 00:28:46,870 So let's see. 416 00:28:47,790 --> 00:28:51,270 So we're trying to pivot this to a new key frame. 417 00:28:51,270 --> 00:28:56,430 So we get our key frame dot new here based on this position. 418 00:28:57,200 --> 00:29:04,070 And I believe what we actually need to do is we should copy this here. 419 00:29:05,540 --> 00:29:09,200 And we're going to place that inside of our C frame. 420 00:29:09,200 --> 00:29:09,950 So. 421 00:29:10,760 --> 00:29:11,360 To that. 422 00:29:11,360 --> 00:29:12,500 We'll do it right here. 423 00:29:13,170 --> 00:29:16,020 And then we multiply it by the keyframed angles. 424 00:29:16,020 --> 00:29:19,590 So I believe we just had a little bit of an error with our parentheses. 425 00:29:19,590 --> 00:29:24,630 But now we should actually be passing a keyframe with a random rotation. 426 00:29:24,630 --> 00:29:26,400 So let's go ahead and try that out again. 427 00:29:28,290 --> 00:29:32,610 So we'll test it out here and we'll go to view it on the server. 428 00:29:32,610 --> 00:29:33,390 And perfect. 429 00:29:33,390 --> 00:29:34,140 There we go. 430 00:29:34,140 --> 00:29:37,680 As you can see our money items have randomly generated on the map. 431 00:29:37,800 --> 00:29:45,180 And you can also see that each of our money items, despite their weird size here they are flush with 432 00:29:45,180 --> 00:29:47,520 the surface that our position was set to. 433 00:29:47,520 --> 00:29:52,440 And that's because we did a little bit of that math calculation to make sure that our objects are flush 434 00:29:52,440 --> 00:29:54,690 with the surface that they need to go onto. 435 00:29:54,690 --> 00:29:56,970 So as you can see, everything looks good. 436 00:29:56,970 --> 00:30:00,120 We have our money items randomly placed all around the map. 437 00:30:00,120 --> 00:30:01,290 There's one down there. 438 00:30:01,290 --> 00:30:03,330 We should have some other ones in here. 439 00:30:03,330 --> 00:30:04,680 There's one right there. 440 00:30:04,710 --> 00:30:06,630 There should be one in the restroom as well. 441 00:30:06,630 --> 00:30:07,680 There's one. 442 00:30:08,590 --> 00:30:11,140 And we should have our other money items. 443 00:30:11,140 --> 00:30:13,030 If I move my mouse around, there should be one. 444 00:30:13,030 --> 00:30:15,850 Yep, there's one right there and there should be one right here. 445 00:30:15,850 --> 00:30:16,540 Perfect. 446 00:30:16,690 --> 00:30:20,950 So all of our money items look like they have successfully generated on our map. 447 00:30:21,580 --> 00:30:23,740 So we're back on the client. 448 00:30:23,740 --> 00:30:27,100 We're going to have to wait for our client to show back up at the restaurant. 449 00:30:28,110 --> 00:30:33,360 Alrighty, now we should be able to protest whether or not we get that money collection guy thing on 450 00:30:33,360 --> 00:30:33,930 our screen. 451 00:30:33,930 --> 00:30:37,320 So let's go ahead and find a money item like this one over here. 452 00:30:37,320 --> 00:30:41,160 If we click it perfect, we get plus $2. 453 00:30:41,160 --> 00:30:43,950 And as you can see, the guy popped up on our screen. 454 00:30:45,690 --> 00:30:46,650 Got $5. 455 00:30:46,650 --> 00:30:50,070 And as you can see, it's also adding it to the leaderboard. 456 00:30:50,070 --> 00:30:53,220 So I add this one gives me my money plus five. 457 00:30:53,220 --> 00:30:55,440 We collect this one plus 15. 458 00:30:55,440 --> 00:30:57,990 Collect this one down here plus 25. 459 00:30:57,990 --> 00:30:58,920 Very nice. 460 00:30:59,460 --> 00:31:04,110 And actually another thing I could test real quick is to see whether or not we get that little message 461 00:31:04,110 --> 00:31:05,220 on the bottom of our screen. 462 00:31:05,220 --> 00:31:07,710 Because this door is locked, it should tell us that the door is locked. 463 00:31:07,710 --> 00:31:08,760 So if we interact. 464 00:31:12,680 --> 00:31:13,070 Huh? 465 00:31:13,220 --> 00:31:19,040 It's not displaying the message, but we are getting the sound played on our end. 466 00:31:19,100 --> 00:31:25,760 So there might be an issue with the little, uh, text at the bottom of our screen. 467 00:31:25,760 --> 00:31:28,160 So let's actually verify this real quick. 468 00:31:28,160 --> 00:31:34,850 So inside of our main guy handler, we should go to our display small message function. 469 00:31:35,540 --> 00:31:36,500 Here we go. 470 00:31:36,740 --> 00:31:38,300 Let's see if there's any issues. 471 00:31:39,470 --> 00:31:45,320 Um, well, we should be fading the element to be opaque, so it should be visible on our screen. 472 00:31:45,320 --> 00:31:49,220 Let's go ahead and look inside of our main GUI and look at our small message label. 473 00:31:50,380 --> 00:31:51,610 Oh, and that's the problem. 474 00:31:51,730 --> 00:31:55,180 The visibility for our small message label is disabled. 475 00:31:55,180 --> 00:32:00,670 So what we can do in here is we can make sure it's enabled by referencing our small message label and 476 00:32:00,670 --> 00:32:02,890 make sure the visibility is set to true. 477 00:32:02,920 --> 00:32:04,600 That should fix our problem now. 478 00:32:05,340 --> 00:32:08,010 So let me go ahead and verify everything else. 479 00:32:08,010 --> 00:32:08,520 Looks good. 480 00:32:08,520 --> 00:32:08,910 All right. 481 00:32:08,910 --> 00:32:09,990 Everything else looks good. 482 00:32:09,990 --> 00:32:11,490 Let's go ahead and try that out again. 483 00:32:12,570 --> 00:32:13,110 All right. 484 00:32:13,110 --> 00:32:16,020 If we go interact with the door there we go. 485 00:32:16,020 --> 00:32:17,040 We get our little message. 486 00:32:17,040 --> 00:32:18,810 The door is locked. 487 00:32:18,810 --> 00:32:22,620 If we try to do it again, it fades it out and pops that one back in. 488 00:32:22,620 --> 00:32:27,750 If we keep spamming it, you can see it keeps overriding the previous messages. 489 00:32:27,750 --> 00:32:28,590 Perfect. 490 00:32:28,590 --> 00:32:31,680 So it looks like our small message system is working great as well. 491 00:32:32,370 --> 00:32:34,500 Otherwise that's all I have for you in this lecture. 492 00:32:34,500 --> 00:32:36,030 I'll see you in the next one.